package com.dkm.ec.product.repository.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.product.domain.ProdCate;
import com.dkm.ec.product.persistence.dao.ProdCateQueryDao;
import com.dkm.ec.product.persistence.entity.ProdCatePo;
import com.dkm.ec.product.repository.ProdCateRepository;

/**
 * 对象功能:产品从属分类表 Repository接口的实现类
 * 开发公司:SKG.COM
 * 开发人员:HJD
 * 创建时间:2015-07-20 11:37:18
 */
@Repository
public class ProdCateRepositoryImpl extends AbstractRepository<String, ProdCatePo,ProdCate> implements ProdCateRepository{
	  
	@Resource
	private  ProdCateQueryDao prodCateQueryDao;

	public ProdCate newInstance() {
		ProdCatePo po = new ProdCatePo();
		ProdCate prodCate = SpringHelper.getBean(ProdCate.class);
		prodCate.setData(po);
		return prodCate;
	}

	public ProdCate newInstance(ProdCatePo po) {
		ProdCate prodCate = SpringHelper.getBean(ProdCate.class);
		prodCate.setData(po);
		return prodCate;
	} 
	
	@Override
	protected IQueryDao<String, ProdCatePo> getQueryDao() {
		return prodCateQueryDao;
	}

	@Override
	public List<ProdCatePo> getProdCateWithCateList(String prodId) {
		return prodCateQueryDao.getProdCateWithCateList(prodId);
	}

	@Override
	public Map<String, ProdCatePo> getCateString(String prodId) {
		Map<String, ProdCatePo> cateMap = new HashMap<String, ProdCatePo>();
		List<ProdCatePo> prodCatePos = getProdCateWithCateList(prodId);
		ProdCatePo mainProdCatePo = new ProdCatePo();
		ProdCatePo subProdCatePo = new ProdCatePo();
		ProdCatePo othersProdCatePo = new ProdCatePo();
		StringBuilder[] cateIdsArr = {new StringBuilder(),new StringBuilder(),new StringBuilder()};
		StringBuilder[] cateNamesArr ={new StringBuilder(),new StringBuilder(),new StringBuilder()};
		String[] tep_cateIds = new String[3];
		String[] tep_cateNames = new String[3];
		ProdCatePo prodCatePo = null;
		if(prodCatePos!=null && prodCatePos.size()>0){
			for(int i=0;i<prodCatePos.size();i++){
				prodCatePo = prodCatePos.get(i);
				if("main".equals(prodCatePo.getCateType())){
					cateIdsArr[0].append(prodCatePo.getCateId()).append(";");
					cateNamesArr[0].append(prodCatePo.getCateName()).append(";");
				}else	if("sub".equals(prodCatePo.getCateType())){
					cateIdsArr[1].append(prodCatePo.getCateId()).append(";");;
					cateNamesArr[1].append(prodCatePo.getCateName()).append(";");;
				}else if("others".equals(prodCatePo.getCateType())){
					cateIdsArr[2].append(prodCatePo.getCateId()).append(";");;
					cateNamesArr[2].append(prodCatePo.getCateName()).append(";");;
				}
			}
			for(int i=0;i<cateIdsArr.length;i++){
				int len = cateIdsArr[i].toString().length();
				if(len>0 && !"".equals(cateIdsArr[i])){
					tep_cateIds[i]= cateIdsArr[i].toString().substring(0, len-1);
				}
			}
			for(int i=0;i<cateNamesArr.length;i++){
				int len = cateNamesArr[i].length();
				if(len>0 && !"".equals(cateNamesArr[i])){
					tep_cateNames[i] =cateNamesArr[i].toString().substring(0, len-1);
				}
			}
		}
		mainProdCatePo.setCateIds(tep_cateIds[0]);
		mainProdCatePo.setCateNames(tep_cateNames[0]);
		subProdCatePo.setCateIds(tep_cateIds[1]);
		subProdCatePo.setCateNames(tep_cateNames[1]);
		othersProdCatePo.setCateIds(tep_cateIds[2]);
		othersProdCatePo.setCateNames(tep_cateNames[2]);
		cateMap.put("main", mainProdCatePo);
		cateMap.put("sub", subProdCatePo);
		cateMap.put("others", othersProdCatePo);
		return cateMap;
	}

	@Override
	public List<String> getProdCateIdListByProdId(String prodId) {
		List<String> cateIds = new ArrayList<String>();
		List<ProdCatePo> prodCatePos = this.getProdCateWithCateList(prodId);
		if(prodCatePos != null){
			for(ProdCatePo prodCatePo : prodCatePos){
				cateIds.add(prodCatePo.getCateId());
			}
		}
		return cateIds;
	}


}
